package com.easy.controllor;

import com.easy.annotation.LogMethod;
import com.easy.bean.PasswordParam;
import com.easy.util.Page;
import com.easy.util.Result;
import com.easy.bean.User;
import com.easy.service.UserService;
import com.easy.util.SYS;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin//允许跨域访问
@RequestMapping("/user")
public class UserControllor {

    @Autowired
    UserService userService;

    @PostMapping
    public Result add(@RequestBody User user){
        Object obj = userService.add(user);
        return Result.success(obj);
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj = userService.del(id);
        return Result.success("删除成功",obj);
    }

    @LogMethod(serviceName = "启用日志：修改用户信息")
    @PutMapping
    public Result edit(@RequestBody User user){
        Object obj = userService.edit(user);
        return Result.success("重置信息成功",obj);
    }
    //修改密码
    @PutMapping("/password")
    public Result edit(@RequestBody PasswordParam param, HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        String s = userService.updatePassword(token, param);
        if(s.equals("旧密码错误"))
            return Result.fail(402,"旧密码错误");
        return Result.success(s);
    }
    @PutMapping("/unknown")
    public Result unknown(@RequestBody PasswordParam param, HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        String s = userService.getEmailCaptcha(token, param);
        return Result.success(s);
    }
    @PutMapping("/unknown/update")
    public Result unknownUpdate(@RequestBody PasswordParam param, HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        String s = userService.unknownUpdate(token, param);
        if(s.equals("邮箱验证码错误"))
            return Result.fail(402,"邮箱验证码错误");
        return Result.success(s);
    }

    @GetMapping
    public Result all(@RequestBody User user){
        Object obj = userService.all(user);
        return Result.success(obj);
    }

    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj = userService.one(id);
        return Result.success(obj);
    }

    @GetMapping("/page")
    public Result page(Page page,User user){
        //查询一共多少条数据
        //查询当前页要显示的数据
        //封装到page里面去
        userService.page(page,user);
        //返回page对象
        return Result.success(page);
    }

    @GetMapping("/token")
    public Result getByToken(HttpServletRequest request){
         String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
         User user = userService.getByToken(token);
         System.out.println(user);
        return Result.success(user);
    }



//手动触发定时任务
    @GetMapping("/deleteImageUtil")
    public void deleteImageUtil(){
        userService.deleteImageUtil();
    }


}
